<template>
    <div class="shopcar-container">
        <!-- 商品列表区域 -->
        <div class="goods-list" v-for="(item,i) in goodslist" :key="item.id">
            <div class="mui-card">
                <div class="mui-card-content">
                    <div class="mui-card-content-inner">
                        <mt-switch v-model="$store.getters.getGoodsSelected[item.id]" @change="selectedChanged(item.id,$store.getters.getGoodsSelected[item.id])"></mt-switch>
                        <img :src="item.thub_path" />
                        <div class="info">
                            <h1>{{item.title}}</h1>
                            <p>
                                <span class="price">￥{{item.sell_price}}</span>
                                <numbox :initcount="$store.getters.getGoodsCount[item.id]" :goodsid="item.id"></numbox>
                                <a href="#" @click.prevent="remove(item.id,i)">删除</a>
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!--结算区域-->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner jiesuan">
                    <div class="left">
                        <p>总计（不含运费）</p>
                        <p>以勾选商品 <span class="red">{{$store.getters.getGoodsCountAndAmount.count}}</span> 件，总价 <span class="red">￥{{$store.getters.getGoodsCountAndAmount.amount}}</span></p>
                    </div>
                    <mt-button type="danger">去结算</mt-button>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import numbox from '../subcomponents/shopcar_numbox.vue'
export default {
    data(){
        return{
            goodslist:[]//购物车中所有商品的数据
        }
    },
    created(){
        this.getGoodsList()
    },
    methods: {
        getGoodsList(){
            this.$http.get('./json/carlist.json').then(result => {
              //1.获取store中所有商品的id，然后拼接出一个用逗号隔开的字符串
              /*var idArr = []
              this.$store.state.car.forEach(item => idArr.push(item.id));*/
              var idArr = []
              idArr = this.$store.state.car;
              if(idArr.length <= 0){return}//如果购物车中没有数据则停止运行
              if(result.body.status === 0){
                  //成功，把数据保存到data上
                  //this.goodslist = result.body.message //获取所有购物车中商品列表
                  var messageList = result.body.message
                  this.goodslist = this.myFilter(messageList,idArr)
                  console.log(idArr)
              }
            })
        },
        /*myFilter2(arr1,arr2) {
            var ret = [];
            arr2.forEach(ele => {
                    var findEle = arr1.find(x => x.id == ele.id );
                    // 如果在arr1中找到,添加到arr1中
                    findEle ? ret.push(findEle) : '';
            });
            return ret;
        }*/
        myFilter(arr1,arr2) {//筛选出所有arr1中含有arr2.id 
            return  arr1.filter((ele) => 
                arr2.filter((x) => x.id == ele.id).length > 0
            );
        },
        remove(id,index){
            //点击删除，把商品从 store 中根据传递的ID删除，同时按index索引将goodslist中的数据删除
            this.goodslist.splice(index,1)
            this.$store.commit("removeFromCar",id)
        },
        selectedChanged(id,val){
            //每当点击开关，获取最新的选中状态
            this.$store.commit("updateGoodsSelected",{id,selected:val})
        }
    },
    components:{//注册挂载组件
        numbox
    }
}
</script>
<style lang="less" scoped>
.shopcar-container{
    background-color:#eee;
    overflow: hidden;
    .goods-list{
        .mui-card-content-inner{
            display:flex;
            justify-items: center;
        }
        img{
            width:60px;
            height:60px;
        }
        h1{
            font-size:13px;
        }
        .info{
            display:flex;
            flex-direction: column;
            justify-content: space-between;
            p{
                display:flex;
                .price{
                    color:#ff5000;
                    font-weight:bold;
                }
            }
        }
    }
    .jiesuan{
        display:flex;
        justify-content: space-between;
        align-items: center;
        font-size:12px;
        .red{
            color:red;
            font-weight: bold;
        }
    }
}
</style>